package top.zerozoo.learn.grade.service.impl;

import top.zerozoo.learn.grade.dao.CourseScoreDao;
import top.zerozoo.learn.grade.entity.CourseScore;
import top.zerozoo.learn.grade.entity.Student;
import top.zerozoo.learn.grade.entity.StudentBaseInfo;
import top.zerozoo.learn.grade.factory.DaoFactory;
import top.zerozoo.learn.grade.service.CourseScoreService;
import top.zerozoo.learn.grade.vo.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class CourseScoreServiceImpl implements CourseScoreService {

    private final CourseScoreDao courseScoreDao = DaoFactory.get(CourseScoreDao.class);

    @Override
    public List<CourseScore> findByStudentID(int id) {
        if (id <= 0) {
            return null;
        }
        return courseScoreDao.findByStudentID(id);
    }


    @Override
    public PageInfo<CourseScore> find(int currentPage, int rows, QueryScoreCondition condition) {
        currentPage = Math.max(currentPage, 1);
        rows = Math.max(rows, 1);

        final PageInfo<CourseScore> pageInfo = new PageInfo<>();

        final int start = (currentPage - 1) * rows;
        final List<CourseScore> courseScores = courseScoreDao.find(start, rows, condition);

        final int totalCount = courseScoreDao.count(condition);
        final int totalPage = (totalCount % rows == 0) ? (totalCount / rows) : (totalCount / rows) + 1;

        pageInfo.setList(courseScores);
        pageInfo.setCurrentPage(currentPage);
        pageInfo.setTotalCount(totalCount);
        pageInfo.setTotalPage(totalPage);

        return pageInfo;
    }
    @Override
    public boolean update(CourseScore courseScore) {
        return courseScoreDao.update(courseScore) != 0;
    }

    @Override
    public boolean insert(CourseScore courseScore) {
        return courseScoreDao.insert(courseScore) != 0;
    }

    @Override
    public boolean delete(int id) {
        if (id < 0) {
            return false;
        }
        return courseScoreDao.deleteById(id) != 0;
    }
}
